<?php
$tlabelReq = 'PORTFOL';
$msgList   = array();
$gidReset  = true;

if ( isset($_REQUEST['cmd']) ) $cmd = $_REQUEST['cmd'];
else                           $cmd = null;

/**
 *  CLAROLINE MAIN SETTINGS
 */

require '../../claroline/inc/claro_init_global.inc.php';
require_once get_path('incRepositorySys').'/lib/user.lib.php';
require_once 'lib/portfolio.lib.php';
require_once 'lib/classes/modele.class.php';

// Gestion de l'objet modele
$modele = new Modele();
$modele->load($_REQUEST['modele_id']);

add_module_lang_array($tlabelReq);

$is_allowedToEdit  = claro_is_allowed_to_edit();

$nameTools = "Portfolio";

$QUERY_STRING=''; // used for the breadcrumb 
                  // when one need to add a parameter after the filename


if (!$is_allowedToEdit)
claro_redirect('evaluation.php?modele_id='.$_REQUEST['modele_id']);

// Fonctions javascript
$htmlHeadXtra[] = '<script type="text/javascript" src="lib/portfolio.js"></script>';
$htmlHeadXtra[] = '<link rel="stylesheet" type="text/css" href="css/portfolio.css" />';


// Inscription
if ($_REQUEST['edit'] == 'yes')
{
	// Utilisateur unique
	if ($_REQUEST['type'] == 'user')
	{
		// On supprime les users
		$modele->supprimerUsers();
		
		if ($_REQUEST['ingroup'])
		{
			foreach ($_REQUEST['ingroup'] as $user)
			{
				if (!$modele->ajouterUser($user))
				$probleme = true;
			}
		}
	}
	
	if ($_REQUEST['type'] == 'class')
	{
		// On supprime les users
		$modele->supprimerClasses();
		
		if ($_REQUEST['ingroup'])
		{
			foreach ($_REQUEST['ingroup'] as $class)
			{
				if (!$modele->ajouterClasse($class))
				$probleme = true;
			}
		}
	}

	if (!$probleme)
	$dialogBox .= get_lang('Record successful');
	else
	$dialogBox .= get_lang('Record failed');
}

// Voir une fiche 
if ($_REQUEST['display'] == 'yes')
{
	claro_redirect('evaluation.php?modele_id='.$_REQUEST['modele_id'].'&user_id='.$_REQUEST['user_id']);
}

include get_path('incRepositorySys') . '/claro_init_header.inc.php';

//$titleElement['mainTitle'] = get_lang("Portfolio");
echo claro_html_tool_title($modele->getNom().'  : <small>'.get_lang('User management').'</small>',false);

if ($dialogBox)
{
	echo claro_html_message_box($dialogBox);
}

echo  "<a class='claroCmd' href=\"entry.php\">"
	     ."<img src=\"img/back.gif\" alt=\"\"> "
	            .get_lang('Back to list')
	            ."</a>\n";

// Seul le créateur a le droit d'ajouter des utilisateurs
// CONF ??
if ($modele->getCreateur() == $_uid || $is_platformAdmin)
{
	echo  " | <a class='claroCmd' href=\"".$_SERVER['PHP_SELF']."?modele_id=".$_REQUEST['modele_id']."&view=class\">"
	     ."<img src=\"".$imgRepositoryWeb."group.gif\" alt=\"\"> "
	            .get_lang('Enrol class')
	            ."</a>\n";
	            
	echo  " | <a class='claroCmd' href=\"".$_SERVER['PHP_SELF']."?modele_id=".$_REQUEST['modele_id']."&view=user\">"
	     ."<img src=\"".$imgRepositoryWeb."group.gif\" alt=\"\"> "
	            .get_lang('Enroll a user')
	            ."</a>\n";

	 // Initialisation
	 $inscrits = array();
	 $etudiants = array();
	            
	 if ($_REQUEST['view'] == 'user')
	{
		// Liste des inscrits
		$listeInscrits = $modele->chercherListeInscrits();
		
		foreach ($listeInscrits as $inscrit)
		{
			$user_id = $inscrit['user_id'];
			$infos_user = user_get_properties($user_id);
			
			if ($user_id)
			$inscrits[$user_id] = $infos_user['lastname'].' '.$infos_user['firstname'];
		}
		
		// Liste des membres du cours
		$listeEtudiants = chercherMembresCours($_cid);
		foreach ($listeEtudiants as $etudiant)
		{
			$user_id = $etudiant['user_id'];
	
			if ($inscrits[$user_id])
			continue;
			
			$etudiants[$user_id] = $etudiant['nom'].' '.$etudiant['prenom'];
		}

		// Titre select
		$register = get_lang("Users to register");
		$available = get_lang("Course users");
	}
	elseif ($_REQUEST['view'] == 'class')
	{
		require_once get_path('incRepositorySys').'/lib/class.lib.php';
		require_once './lib/CLFDclass.lib.php';
		
		// Liste des classes inscrites
		$classesInscrites = $modele->chercherClassesInscrites();
		foreach ($classesInscrites as $classe)
		{
			$classe_id = $classe['class_id'];
			$classe_info = CLFDclass_get_properties ( $classe_id );
			
			if ($classe_id)
			$inscrits[$classe_id] = $classe_info['name'].' - '.$classe_info['description'];
		}
		
		// Liste des classes membres du cours
		$listeClassesCours = CLFDget_class_list_by_course($_cid);

		foreach ($listeClassesCours as $classe)
		{
			$classe_id = $classe['id'];
			$classe_info = CLFDclass_get_properties ( $classe_id );
			
			if ($inscrits[$classe_id])
			{
				continue;
			}
			$etudiants[$classe_id] = $classe_info['name'].' - '.$classe_info['description'];;
		}

		// Titre select
		$register = get_lang("Classes to register");
		$available = get_lang("Course classes");
	}

	if ($_REQUEST['view'])
	{
		echo '<form name="groupedit" method="POST" action="' . $_SERVER['PHP_SELF'] . '?edit=yes&modele_id='.$_REQUEST['modele_id'].'" class="groupedit">' . "\n"
		.	'<input type="hidden" name="type" value="'.$_REQUEST['view'].'">'
		.    '<table border="0" cellspacing="3" cellpadding="5">' . "\n"
		.	  '<tr valign="top">'
		.    '<th>'
		.    '<label for="inGroup">' . $register . '</label>'
		.    ' : '
		.    '</th>' . "\n"
		.    '<th></th>'. "\n"
		.    '<th>'
		.    '<label for="inGroup">' . $available . '</label>'
		.    ' : '
		.    '</th>' . "\n"
		.    '</tr>' . "\n"
		.    '<tr>' . "\n"
		.    '<td>'
		.    claro_html_form_select('ingroup[]',$inscrits,'',array('id'=>'ingroup', 'size'=>'8', 'multiple'=>'multiple'),true)
		.    '</td>' . "\n"
		.    '<td>' . "\n"
		.    '<input type="button" onClick="move(this.form.elements[\'ingroup\'],this.form.elements[\'nogroup\'])" value="   >>   " />' . "\n"
		.    '<br />' . "\n"
		.    '<input type="button" onClick="move(this.form.elements[\'nogroup\'],this.form.elements[\'ingroup\'])" value="   <<   " / >' . "\n"
		.    '</td>' . "\n"
		.    '<td>' . "\n"
		.    claro_html_form_select('nogroup[]',$etudiants,'',array('id'=>'nogroup', 'size'=>'8', 'multiple'=>'multiple'), true) . "\n"
		.    '</td></tr>' . "\n"
		.    '<tr>' . "\n"
		.    '<td colspan=3>' . "\n"
		.    '<input type=submit value="' . get_lang("Ok") . '" name="modify" onClick="selectAll(this.form.elements[\'ingroup\'],true)" style="width:100%"/>' . "\n"
		.    '</td>' . "\n"
		.	  '</table>'
		;
	}
}

// Les autres admin du cours / évaluateurs ont accès à la fiche des users
// CONF ??
if (!$_REQUEST['view'])
{
	require_once get_path('incRepositorySys').'/lib/class.lib.php';
	
	echo "<h5>".get_lang('Display portfolio')."</h5>";

	$userSeuls=array();

	// Utilisateurs uniques
	foreach ($modele->chercherListeInscrits() as $user)
	{
		$user_id = $user['user_id'];
		$infos_user = user_get_properties($user_id);
		
		$inscrits[] = array('user_id' => $user_id, 'nom' => $infos_user['lastname'], 'prenom' =>$infos_user['firstname']);

		$userSeuls[$user_id] = 1;
	}
	
	// Classes	
	foreach ($modele->chercherClassesInscrites() as $class)
	{
		$users = chercherUsersClasse($class['class_id']);
		
		foreach ($users as $user)
		{
			if (in_array($user['user_id'],$userSeuls))
			continue;
			
			$inscrits[] = array('user_id' => $user['user_id'], 'nom' => $user['nom'], 'prenom' =>$user['prenom']);
		}
	}

	// TRI par nom, prénom
	foreach ($inscrits as $key => $row) {
	    $user_id[$key]  = $row['user_id'];
	    $nom[$key] = $row['nom'];
		$prenom[$key] = $row['prenom'];
	}
	array_multisort($nom, SORT_ASC, $prenom, SORT_ASC, $inscrits);
	
	echo '<form method="POST" action="' . $_SERVER['PHP_SELF'] . '?display=yes&modele_id='.$_REQUEST['modele_id'].'">' . "\n"
		   .'<select name="user_id">';
	
	foreach ($inscrits as $inscrit)
	{
		echo '<option value="'.$inscrit['user_id'].'">'.$inscrit['nom'].' '.$inscrit['prenom'].'</option>';
	}
	
	echo '</select>'
		    .'<input type="submit" value="OK">'
			.'</form>';
}



include get_path('incRepositorySys').'/claro_init_footer.inc.php';
?>
